<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="日期" prop="classDate">
        <el-date-picker
          v-model="formData.classDate"
          type="date"
          value-format="x"
          placeholder="选择日期"
        />
      </el-form-item>
      <el-form-item label="时间段" prop="classTime">
        <el-date-picker
          v-model="formData.classTime"
          type="date"
          value-format="x"
          placeholder="选择时间段"
        />
      </el-form-item>
      <el-form-item label="课程" prop="courseName">
        <el-input v-model="formData.courseName" placeholder="请输入课程" />
      </el-form-item>

      <el-form-item label="授课老师" prop="teacherName">
        <el-input v-model="formData.teacherName" placeholder="请输入授课老师" />
      </el-form-item>

      <el-form-item label="巡班老师" prop="patrolTeacherName">
        <el-input v-model="formData.patrolTeacherName" placeholder="请输入巡班老师" />
      </el-form-item>
      <el-form-item label="教室" prop="classroom">
        <el-input v-model="formData.classroom" placeholder="请输入教室" />
      </el-form-item>
      <el-form-item label="类型" prop="violationType">
        <el-select v-model="formData.violationType" placeholder="请选择类型">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="描述" prop="violationDesc">
        <el-input v-model="formData.violationDesc" placeholder="请输入描述" />
      </el-form-item>
      <el-form-item label="人数" prop="violationCount">
        <el-input v-model="formData.violationCount" placeholder="请输入人数" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="formData.status">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { PatrolApi, Patrol } from '@/api/ao/patrol'

/** 巡班违纪记录 表单 */
defineOptions({ name: 'PatrolForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  classDate: undefined,
  classTime: undefined,
  courseName: undefined,

  teacherName: undefined,

  patrolTeacherName: undefined,
  classroom: undefined,
  violationType: undefined,
  violationDesc: undefined,
  violationCount: undefined,
  status: undefined,
})
const formRules = reactive({
  classDate: [{ required: true, message: '日期不能为空', trigger: 'blur' }],
  classTime: [{ required: true, message: '时间段不能为空', trigger: 'blur' }],
  courseName: [{ required: true, message: '课程不能为空', trigger: 'blur' }],

  teacherName: [{ required: true, message: '授课老师不能为空', trigger: 'blur' }],

  patrolTeacherName: [{ required: true, message: '巡班老师不能为空', trigger: 'blur' }],
  classroom: [{ required: true, message: '教室不能为空', trigger: 'blur' }],
  violationType: [{ required: true, message: '类型不能为空', trigger: 'change' }],
  violationCount: [{ required: true, message: '人数不能为空', trigger: 'blur' }],
  status: [{ required: true, message: '状态不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await PatrolApi.getPatrol(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as Patrol
    if (formType.value === 'create') {
      await PatrolApi.createPatrol(data)
      message.success(t('common.createSuccess'))
    } else {
      await PatrolApi.updatePatrol(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    classDate: undefined,
    classTime: undefined,
    courseName: undefined,

    teacherName: undefined,

    patrolTeacherName: undefined,
    classroom: undefined,
    violationType: undefined,
    violationDesc: undefined,
    violationCount: undefined,
    status: undefined,
  }
  formRef.value?.resetFields()
}
</script>